package org.example.domain.strategy.service.raffle;

import org.example.domain.strategy.model.entity.RaffleFactorEntity;
import org.example.domain.strategy.model.entity.RuleActionEntity;
import org.example.domain.strategy.model.entity.RuleMatterEntity;
import org.example.domain.strategy.model.vo.RuleLogicCheckTypeVO;
import org.example.domain.strategy.service.rule.ILogicFilter;
import org.example.domain.strategy.service.rule.factory.DefaultLogicFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.Set;

@Service
public class DefaultRaffleStrategy extends AbstractRaffleStrategy{
    @Resource
    private DefaultLogicFactory logicFactory;

    @Override
    protected RuleActionEntity<RuleActionEntity.RaffleCenterEntity> doCheckRaffleCenterLogic(RaffleFactorEntity raffleFactorEntity, List<String> logics) {
        Map<String, ILogicFilter<RuleActionEntity.RaffleCenterEntity>> filterMap = logicFactory.centerLogicFilterMap;
        RuleActionEntity<RuleActionEntity.RaffleCenterEntity> centerRuleActionEntity = null;
        for (String logic : logics) {
            ILogicFilter<RuleActionEntity.RaffleCenterEntity> logicFilter = filterMap.get(logic);
            if(null == logicFilter) continue;
            RuleMatterEntity ruleMatterEntity = RuleMatterEntity.builder()
                    .userId(raffleFactorEntity.getUserId())
                    .strategyId(raffleFactorEntity.getStrategyId())
                    .awardId(raffleFactorEntity.getAwardId())
                    .ruleModel(logic)
                    .build();
            centerRuleActionEntity = logicFilter.filter(ruleMatterEntity);
            if (RuleLogicCheckTypeVO.TAKE_OVER.getCode().equals(centerRuleActionEntity.getCode())) {
                return centerRuleActionEntity;
            }
        }

        return centerRuleActionEntity;
    }
}
